﻿using System;
using System.Web.Mvc;

using mvcForum.AddOns.ViewModels;

using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Interfaces.AddOns;
using mvcForum.Core.Interfaces.Data;

using mvcForum.Web.Controllers;
using mvcForum.Web.Helpers;
using mvcForum.Web.Interfaces;
using mvcForum.AddOns.StopForumSpam;

namespace mvcForum.AddOns.Controllers {

	public class StopForumSpamConfigurationController : ForumAdminBaseController, IAntiSpamConfigurationController {
		private readonly StopForumSpamConfiguration config;

		public StopForumSpamConfigurationController(IWebUserProvider userProvider, IContext context, IAddOnConfiguration<StopForumSpamAddOn> config)
			: base(userProvider, context) {

			this.config = (StopForumSpamConfiguration)config;
		}

		public ViewResult Index() {
			return View(new StopForumSpamViewModel { Key = this.config.Key, CheckNewUsers = this.config.CheckNewUsers, MarkAsSpamOnHit = this.config.MarkAsSpamOnHit, RunAsynchronously = this.config.RunAsynchronously, Delay = this.config.Delay, Enabled = this.config.Enabled });
		}

		[HttpPost]
		public ViewResult Index(StopForumSpamViewModel model) {
			if (ModelState.IsValid) {
				this.config.CheckNewUsers = model.CheckNewUsers;
				this.config.Key = model.Key;
				this.config.MarkAsSpamOnHit = model.MarkAsSpamOnHit;
				this.config.RunAsynchronously = model.RunAsynchronously;
				this.config.Delay = model.Delay;
				this.config.Enabled = model.Enabled;

				this.Context.SaveChanges();

				TempData["Saved"] = true;
			}

			return View(model);
		}

		public String Name {
			get {
				return ForumHelper.GetString("Name", null, "mvcForum.AddOns.StopForumSpam");
			}
		}

		public String Description {
			get {
				return ForumHelper.GetString("Description", null, "mvcForum.AddOns.StopForumSpam");
			}
		}
	}
}